/* site-wide search functionality */
$(document).ready(function(){
	$("#menusearch").click(function(){
		if($("#searchbar").is(":visible")){
			$("#searchbar").slideUp(500);
		}else{
			$("#searchbar").slideDown(500);
			$("#searchfieldtext").focus();
		}
	});

	$("#searchbutton").click(function(){
		searchSite();
	});

	$("#searchfieldtext").keyup(function(e){
		if(e.keyCode == 13){
			searchSite();
		}
	});

	/* init the search page when there are results */
	if($("#searchresultitemlist").length>0){
		initializeSearchResult();
	}
});

/* executes the site search */
function searchSite(query){
	var squery = query;
	if(!squery){
		if($("#searchfieldtext").val()!=""){
			squery = $("#searchfieldtext").val().trim();
		}	
	}

	location.href="/s/"+squery+"?ga_search_keyword="+escape(squery);
}

/* number of search result pages */
var searchResultPageCount = 0;
var searchResultActivePage = 0;

/* initializes the search result page */
function initializeSearchResult(){
	var pageItemCount = 0;
	var pageCount = 0;

	/* loop through results and add additional classes */
	$(".searchresultitem").each(function(i, obj) {
    	$(obj).attr("id","searchresultitem"+i);
    	$(obj).addClass("searchresultitempage"+pageCount);
    	$(obj).addClass("searchresultitempageitem"+pageItemCount);

    	if(pageItemCount==4){
    		pageItemCount=0;
    		
    		/* only increment when there are elements left */
    		if(i!=($(".searchresultitem").length-1)){
    			pageCount++;
    		}
    	}else{
    		pageItemCount++;
    	}
	});

	/* set global number of search pages */
	searchResultPageCount = pageCount+1;

	/* enable pagination */
	paginateSearchResult(0);

	/* fade in the result list */
	$("#searchresultlist").slideDown(500);
}

/* creates the pagination of the search results and also 
	renders the page navigation with back and forth */
function paginateSearchResult(activePage){
	/* only show pagination when there is more than 1 page */
	if(searchResultPageCount>1){
		// set the global var for active page
		searchResultActivePage = parseInt(activePage);

		// create the html for the active page
		var paginationHtml = "<div id='searchresultpagination'>"
						+ "<div id='searchresultnavigateprev'>&laquo;</div>";

		/* p defines the start page in the nav */
		var p = 0;

		/* displayPageCount defines the last page in the nav */
		var displayPageCount = 5;

		/* show maximum 5 pages in the nav */
		if(searchResultPageCount<displayPageCount){
			displayPageCount = searchResultPageCount;
		}

		/* when active page is 5 or greater then 
			show next one and previous 5 */
		if(activePage >= 4){
			p = activePage-3;
			displayPageCount = parseInt(activePage)+2;
		}

		/* when active page is the last in list, then
			show previous 5 pages before in nav */
		if(activePage == (searchResultPageCount-1)
			&&searchResultPageCount > 4){
			p = searchResultPageCount-5;
			displayPageCount = searchResultPageCount;
		}

		while(p<displayPageCount){
			paginationHtml += "<div id='searchresultnav"+p+"' class='searchresultnav'>"+(p+1)+"</div>";
			p++;
		}
		paginationHtml += "<div id='searchresultnavigatenext'>&raquo;</div>"
						+ "</div>";

		/* update the container with the html */
		$("#searchresultnavigation").html(paginationHtml);

		// add the active class to the active page
		$("#searchresultnav"+activePage).addClass("searchresultnavactive");

		/* calculate width for search-nav */
		var widthElementCount = 5;
		if(displayPageCount<5){
			widthElementCount = displayPageCount;
		}
		var navWidth = (widthElementCount*50)+86;
		$("#searchresultpagination").width(navWidth);

		/* show the active page search results */
		$(".searchresultitem").hide();
		$(".searchresultitempage"+activePage).show();

		/* send user to page top */
		$("html, body").animate({ scrollTop: 0 }, "slow");

		/* dont show left nav when on first page */
		if(activePage==0){
			$("#searchresultnavigateprev").css("cursor","default");
			$("#searchresultnavigateprev").css("opacity","0.5");
			$("#searchresultnavigateprev").css("color","#ddd");
		}

		/* dont show right nav when on last page */
		if(activePage==(searchResultPageCount-1)){
			$("#searchresultnavigatenext").css("cursor","default");
			$("#searchresultnavigatenext").css("opacity","0.5");
			$("#searchresultnavigatenext").css("color","#ddd");
		}

		/* attach previous nav link when page is not first one */
		if(activePage>0){
			$("#searchresultnavigateprev").click(function(){
				paginateSearchResult(parseInt(searchResultActivePage)-1);
			});
		}

		/* attach next nav link when page is not last one */
		if(activePage<(searchResultPageCount-1)){
			$("#searchresultnavigatenext").click(function(){
				paginateSearchResult(parseInt(searchResultActivePage)+1);
			});
		}

		/* attach click function to search-nav */
		$(".searchresultnav").click(function(e){
			var resultPageId = e.target.id.substring(15);
			paginateSearchResult(resultPageId);
		});
	}
}